import os
import time

from selenium.webdriver.common.by import By

from Base.base_action import BaseAction


class SkinTest(BaseAction):
    test_btn = (By.ID, "org.c2h4.afei.beauty:id/main_ib_camera")  # 测一测按钮
    test_self = (By.ID, "org.c2h4.afei.beauty:id/btn_self_test")  # 自己测按钮
    # test_for_frind = (By.ID, "org.c2h4.afei.beauty:id/btn_other_test")  # 帮朋友测按钮
    # manual_test = (By.XPATH, "//*[contains(@text, '手动拍照测')]")  # 手动拍照测按钮
    authority_allowed = (By.ID, "org.c2h4.afei.beauty:id/tv_commit")  # 权限允许按钮
    i_know = (By.ID, "org.c2h4.afei.beauty:id/tv_i_know")
    swipe_down = (500, 1500, 500, 500, 500)  # 向下滑动
    begin_test = (By.ID, "org.c2h4.afei.beauty:id/tv_begin_test")  # 开始测一测按钮

    def private_allowed(self):
        while True:
            els = self.driver.find_elements_by_class_name('android.widget.Button')
            if els:
                for el in els:
                    if el.text == '允许':
                        self.driver.find_element_by_android_uiautomator('new UiSelector().text("允许")').click()
                        break
                    elif el.text == '本次运行允许':
                        self.driver.find_element_by_android_uiautomator('new UiSelector().text("本次运行允许")').click()
                        break
                    elif el.text == '仅在使用中允许':
                        self.driver.find_element_by_android_uiautomator('new UiSelector().text("仅在使用中允许")').click()
                        break
                    elif el.text == '始终允许':
                        self.driver.find_element_by_android_uiautomator('new UiSelector().text("始终允许")').click()
                        break
                    elif el.text == '确定':
                        self.driver.find_element_by_android_uiautomator('new UiSelector().text("确定")').click()
                        break
            else:
                break
            time.sleep(1)

    # 目前只写到开启拍照
    def skin_test(self):
        self.click_element(self.test_btn)
        self.click_element(self.test_self)
        if "请平视后置摄像头" not in self.get_page_source():
            try:
                self.click_element(self.authority_allowed)
            except:
                pass
            try:
                self.private_allowed()
            except:
                pass
            try:
                self.click_element(self.test_self)
            except:
                pass
            try:
                self.click_element(self.i_know)
            except:
                pass
            if "视频演示" in self.get_page_source():
                for _ in range(3):
                    self.swipe(*self.swipe_down)
                try:
                    self.click_element(self.begin_test)
                except:
                    pass

            time.sleep(15)
